package com.ls.task.dal.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ls.task.dal.dao.SysTaskTriggerDao;
import com.ls.task.dal.vo.SysTaskTriggerVo;
import com.ls.task.dal.model.SysTaskTrigger;
import com.ls.fw.commons.core.utils.reflect.ClassUtils;
import com.ls.fw.data.rdb.impl.dao.EntityDaoImpl;
import com.ls.fw.data.rdb.impl.mybatis.mapper.entity.Example;
import com.ls.fw.data.exception.DataException;
import com.ls.task.dal.dao.mapper.SysTaskTriggerMapper;
import com.ls.fw.data.rdb.utils.ArraysUtils;
/**
 * 
 * @author ls
 * 2016年08月18日 09时49分16秒
 */
@Repository("sysTaskTriggerDao") 
public class SysTaskTriggerDaoImpl extends EntityDaoImpl<SysTaskTrigger,SysTaskTriggerVo,java.lang.Long> 
                            implements SysTaskTriggerDao{
 
    @Autowired
	private SysTaskTriggerMapper mapper;
	
    @Override
	public SysTaskTriggerMapper getMapper(){
	    return mapper;
    }
    
    @Override
	public List<SysTaskTrigger> findAll() throws DataException {
		return mapper.query(new SysTaskTriggerVo());
	}

	@Override
	public int delete(java.lang.Long id) throws DataException {
		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public int save(SysTaskTrigger model) throws DataException {
		return mapper.insertSelective(model);
	}

	@Override
	public int update(SysTaskTrigger model) throws DataException {
		Example example = new Example(SysTaskTrigger.class);
		example.or().andEqualTo(SysTaskTrigger.Columns.PK.property(),model.getId())
		.andEqualTo(SysTaskTrigger.Columns.canUpdate.property(), true);
		return mapper.updateByExampleSelective(model, example);
//		return mapper.updateByPrimaryKeySelective(model);
	}

	@Override
	public SysTaskTrigger find(java.lang.Long id) throws DataException {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(String... id) throws DataException {
		Example example = new Example(SysTaskTrigger.class);
		example.or().andIn(SysTaskTrigger.Columns.PK.property(),ArraysUtils.asList(id))
		.andEqualTo(SysTaskTrigger.Columns.canDelete.property(), true);
		return mapper.deleteByExample(example );
	}

	@Override
	public int remove(String... id) throws DataException {
	    Example example = new Example(SysTaskTrigger.class);
		example.or().andIn(SysTaskTrigger.Columns.PK.property(),ArraysUtils.asList(id))
		.andEqualTo(SysTaskTrigger.Columns.canUpdate.property(), true)
		;
		SysTaskTrigger record = new SysTaskTrigger();
		record.setDeleteClass(Boolean.TRUE);
		return mapper.updateByExampleSelective(record , example);
	}

	@Override
	public List<SysTaskTrigger> query(SysTaskTriggerVo model)
			throws DataException {
		return this.mapper.query(model);
	}

	@Override
	public int persist(SysTaskTrigger model) {
		Object id = null;
		try {
			id = ClassUtils.getFieldValue(model, SysTaskTrigger.Columns.PK.property());
		} catch (SecurityException e) {
			throw new DataException(e);
		} catch (Exception e) {
			throw new DataException(e);
		}
		if(id==null){
			return this.save(model);
		}else{
			return this.update(model);
		}
	}
}
